{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    #def setZeroes(self, matrix: List[List[int]]) -> None:\n",
    "    def setZeroes(self, matrix) -> None:\n",
    "        \"\"\"\n",
    "        Do not return anything, modify matrix in-place instead.\n",
    "        \"\"\"\n",
    "        s_r = set()\n",
    "        s_c = set()\n",
    "        rows = len(matrix)\n",
    "        cols = len(matrix[0])\n",
    "        if rows == 0 or cols == 0:\n",
    "            return\n",
    "        for i in range(rows):\n",
    "            for j in range(cols):\n",
    "                if(matrix[i][j] == 0):\n",
    "                    s_r.add(i)\n",
    "                    s_c.add(j)\n",
    "        for i in s_r:\n",
    "            for c in range(cols):\n",
    "                matrix[i][c] = 0\n",
    "        for i in s_c:\n",
    "            for r in range(rows):\n",
    "                matrix[r][i] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0, 0, 0, 0], [0, 0, 0, 4], [0, 0, 0, 0], [0, 0, 0, 3], [0, 0, 0, 0]]\n"
     ]
    }
   ],
   "source": [
    "m = [\n",
    "  [0,1,2,0],\n",
    "  [3,4,5,2],\n",
    "  [1,3,1,5]\n",
    "]\n",
    "m=[[0,0,0,5],\n",
    "   [4,3,1,4],\n",
    "   [0,1,1,4],\n",
    "   [1,2,1,3],\n",
    "   [0,0,1,1]]\n",
    "a = Solution()\n",
    "a.setZeroes(m)\n",
    "print(m)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "常数复杂度的，讲一下思路:\n",
    "矩阵中某个数为零，则将该数所在行的第一个数置零，所在列的第一个数置零;\n",
    "这样并不会影响该列该行首个数的取值，因为他们最后都会被置零。\n",
    "\n",
    "利用两个flag记录第一行第一列是否有0\n",
    "遍历除第一行第一列以外的矩阵元素，如果有元素为0，则将对应的第一行第一列的位置置为0\n",
    "同样遍历除第一行第一列以外的矩阵元素，\n",
    "如果matrix[i][j]所在位置的第一行第一列有任意一个位置为0 那么那个位置将被置为0\n",
    "根据两个flag决定是否将第一行第一列置为0"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
